package com.zx.crowdfunding.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.zx.crowdfunding.entity.po.AddressPO;
import com.zx.crowdfunding.entity.po.AddressPOExample;
import com.zx.crowdfunding.entity.po.AddressPOExample.Criteria;
import com.zx.crowdfunding.entity.po.OrderPO;
import com.zx.crowdfunding.entity.po.OrderProjectPO;
import com.zx.crowdfunding.entity.vo.AddressVO;
import com.zx.crowdfunding.entity.vo.OrderProjectVO;
import com.zx.crowdfunding.entity.vo.OrderVO;
import com.zx.crowdfunding.mapper.AddressPOMapper;
import com.zx.crowdfunding.mapper.OrderPOMapper;
import com.zx.crowdfunding.mapper.OrderProjectPOMapper;
import com.zx.crowdfunding.service.api.OrderService;

/**
 * 订单 - Service接口实现类
 * @author 郑雪
 * @date 2022-01-04
 */
@Service
// 针对查询操作设置事务只读属性
@Transactional(readOnly = true)
public class OrderServiceImpl implements OrderService {
	
	/**
	 * 订单Mapper接口
	 */
	@Autowired
	private OrderPOMapper orderPOMapper;
	
	/**
	 * 订单项目Mapper接口
	 */
	@Autowired
	private OrderProjectPOMapper orderProjectPOMapper;
	
	/**
	 * 收货地址Mapper接口
	 */
	@Autowired
	private AddressPOMapper addressPOMapper;

	@Override
	public OrderProjectVO getOrderProjectVO(Integer returnId) {
		return orderProjectPOMapper.selectOrderProjectVO(returnId);
	}

	@Override
	public List<AddressVO> getAddressVOList(Integer memberId) {
		AddressPOExample example = new AddressPOExample();
		Criteria criteria = example.createCriteria();
		criteria.andMemberIdEqualTo(memberId);
		List<AddressPO> addressPOList = addressPOMapper.selectByExample(example);
		List<AddressVO> addressVOList = new ArrayList<>();
		for (AddressPO addressPO : addressPOList) {
			AddressVO addressVO = new AddressVO();
			BeanUtils.copyProperties(addressPO, addressVO);
			addressVOList.add(addressVO);
		}
		return addressVOList;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
	public void saveAddressVO(AddressVO addressVO) {
		AddressPO addressPO = new AddressPO();
		BeanUtils.copyProperties(addressVO, addressPO);
		addressPOMapper.insertSelective(addressPO);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
	public void saveOrderVO(OrderVO orderVO, Integer memberId) {
		
		// 添加订单
		OrderPO orderPO = new OrderPO();
		BeanUtils.copyProperties(orderVO, orderPO);
		orderPO.setMemberId(memberId);
		orderPO.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		orderPOMapper.insertSelective(orderPO);
		
		// 添加订单项目
		OrderProjectPO orderProjectPO = new OrderProjectPO();
		BeanUtils.copyProperties(orderVO.getOrderProjectVO(), orderProjectPO);
		orderProjectPO.setOrderId(orderPO.getId());
		orderProjectPO.setMemberId(memberId);
		orderProjectPOMapper.insertSelective(orderProjectPO);
	}
	

}
